#!/bin/bash

RETRIES="{{ handler_health_nfs_check_retries }}"
DELAY="{{ handler_health_nfs_check_delay }}"
NFS_NAME="ceph-nfs@{{ ceph_nfs_service_suffix | default(ansible_hostname) }}"
PID=/var/run/ganesha.pid
{% if containerized_deployment %}
DOCKER_EXEC="docker exec ceph-nfs-{{ ansible_hostname }}"
{% endif %}

# First, restart the daemon
{% if containerized_deployment -%}
systemctl restart $NFS_NAME
COUNT=10
# Wait and ensure the pid exists after restarting the daemon
while [ $RETRIES -ne 0 ]; do
  $DOCKER_EXEC test -f $PID && exit 0
  sleep $DELAY
  let RETRIES=RETRIES-1
done
# If we reach this point, it means the pid is not present.
echo "PID file ${PID} could not be found, which means Ganesha is not running."
exit 1
{% else %}
systemctl restart nfs-ganesha
{% endif %}
